/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jinatonic.confetti.sample;

import com.github.jinatonic.confetti.*;
import com.github.jinatonic.confetti.confetto.Confetto;
import com.github.jinatonic.confetti.confetto.ShimmeringConfetto;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ShimmeringAbility extends Ability implements Component.ClickedListener, ConfettoGenerator {

    protected ComponentContainer container;

    protected int goldDark, goldMed, gold, goldLight;

    private final List<ConfettiManager> activeConfettiManagers = new ArrayList<>();

    private int size;
    private int velocitySlow, velocityNormal;

    private List<PixelMap> confettoBitmaps;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_confetti);
        getWindow().setStatusBarColor(Color.getIntColor("#946335"));
        container = (ComponentContainer) findComponentById(ResourceTable.Id_container);
        findComponentById(ResourceTable.Id_generate_confetti_once_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_stream_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_infinite_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_generate_confetti_stop_btn).setClickedListener(this);
        findComponentById(ResourceTable.Id_back).setClickedListener(this);
        goldDark = Color.getIntColor("#946335");
        goldMed = Color.getIntColor("#ab804b");
        gold = Color.getIntColor("#cea774");
        goldLight = Color.getIntColor("#edd3b2");
        int[] colors = {Color.BLACK.getValue()};
        size = AttrHelper.vp2px(6, container.getContext());
        velocitySlow = AttrHelper.vp2px(50, container.getContext());
        velocityNormal = AttrHelper.vp2px(100, container.getContext());
        confettoBitmaps = Utils.generateConfettiBitmaps(colors, size);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_generate_confetti_once_btn:
                activeConfettiManagers.add(generateOnce());
                break;
            case ResourceTable.Id_generate_confetti_stream_btn:
                activeConfettiManagers.add(generateStream());
                break;
            case ResourceTable.Id_generate_confetti_infinite_btn:
                activeConfettiManagers.add(generateInfinite());
                break;
            case ResourceTable.Id_generate_confetti_stop_btn:
                for (ConfettiManager confettiManager : activeConfettiManagers) {
                    confettiManager.terminate();
                }
                activeConfettiManagers.clear();
                break;
            case ResourceTable.Id_back:
                terminateAbility();
                break;
        }
    }

    protected ConfettiManager generateOnce() {
        return getConfettiManager().setNumInitialCount(100)
                .setEmissionDuration(0)
                .animate();
    }

    protected ConfettiManager generateStream() {
        return getConfettiManager().setNumInitialCount(0)
                .setEmissionDuration(3000)
                .setEmissionRate(50)
                .animate();
    }

    protected ConfettiManager generateInfinite() {
        return getConfettiManager().setNumInitialCount(0)
                .setEmissionDuration(ConfettiManager.INFINITE_DURATION)
                .setEmissionRate(50)
                .animate();
    }

    private ConfettiManager getConfettiManager() {
        final ConfettiSource confettiSource = new ConfettiSource(0, (int) (velocitySlow-size*2.5f), container.getWidth(),
                (int) (velocitySlow-size*2.5f));
        return new ConfettiManager(this, this, confettiSource, container)
                .setVelocityX(0, velocitySlow)
                .setVelocityY(velocityNormal, velocitySlow)
                .setInitialRotation(180, 180)
                .setRotationalAcceleration(360, 180)
                .setTargetRotationalVelocity(360);
    }

    public Confetto generateConfetto(Random random) {
        return new ShimmeringConfetto(
                confettoBitmaps.get(random.nextInt(confettoBitmaps.size())),
                goldLight, goldDark, 1000, random);
    }
}
